
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author NamPhong
 */
public class RSA_Stype {

    public static void main(String[] args) {
        String strFirst = "Hoang Manh Chuyen";
        System.out.println("Chuoi ban dau: "+strFirst);
        try {
            // Xac dinh thuat toan ma hoa
            Cipher ci = Cipher.getInstance("RSA");
            // Tao key
            KeyPairGenerator kpgt = KeyPairGenerator.getInstance("RSA");
            KeyPair keyPair = kpgt.generateKeyPair();
            // KheyPair co 1 cap( public key va private key)
            PublicKey pubKey = keyPair.getPublic();
            PrivateKey priKey = keyPair.getPrivate();
            
            try {
                // luu key 
                ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("PrivateKey.dat")));
                
            } catch (FileNotFoundException ex) {
                Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
            }
            // ma hoa
            ci.init(Cipher.ENCRYPT_MODE, pubKey);
            // chuyen chuoi so sang chu
            byte[] strByteEn = ci.doFinal(strFirst.getBytes());
            String strEn = Base64.encode(strByteEn);
            System.out.println("Chuoi ma hoa: "+strEn);
            // giai ma
            ci.init(Cipher.DECRYPT_MODE, priKey);
            // chuyen chuoi
            byte[] strByteDe = ci.doFinal(Base64.decode(strEn));
            String strDe = new String(strByteDe);
            System.out.println("Chuoi giai ma: "+strDe);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(RSA_Stype.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
